package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mappers.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    //保存用户信息
    @Override
    public void saveUserInfo(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    //更新用户信息表的数据
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    //查询用户信息
    @Override
    public UserInfo findById(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    //根据ids查询
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> userIds) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.in("id",userIds);
        List<UserInfo> userInfos = userInfoMapper.selectList(wrapper);
        Map<Long, UserInfo> map = new HashMap<>();
        for (UserInfo userInfo : userInfos) {
            map.put(userInfo.getId(),userInfo);
        }
        return map;
    }

    //分页查询所有
    @Override
    public IPage findAll(Integer page, Integer pagesize) {
        Page<UserInfo> userInfoPage = new Page<>(page, pagesize);
        return userInfoMapper.selectPage(userInfoPage,null);
    }
}
